package com.zsy.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.xbw.bean.Bis_Shop;
import com.xbw.bean.Bis_shop_post;
import com.xbw.service.ShopService;
import com.zsy.bean.Bis_Shop_User;
import com.zsy.bean.PageBeanZsy;
import com.zsy.service.Bis_Shop_PostService;
import com.zsy.service.Bis_Shop_UserService;

@Controller
@RequestMapping("bis_shop_user")
public class Bis_Shop_UserController {
	@Autowired
	private Bis_Shop_UserService bis_Shop_UserService;
	
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private Bis_Shop_PostService  bis_Shop_PostService ;
	
	//鍛橀敓鏂ゆ嫹閿熷彨鎲嬫嫹鐗熼敓绐栵拷
	@RequestMapping("/list")
	public String list(Model model ,HttpServletRequest request ,@RequestParam(value="curr",defaultValue="1")int curr ){
		
		
		PageBeanZsy pbz=new PageBeanZsy();
		pbz.setCurrent(curr);
		Map<String, Object> map=initMap(request, null);
		pbz.setSize(3);  // 姣忛〉閿熸枻鎷风ず閿熸枻鎷烽敓鏂ゆ嫹
		//   min 閿熻鍖℃嫹濮嬮敓閾版唻鎷�   閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷� max  閿熻鏂ゆ嫹閿熸枻鎷烽敓閾版唻鎷�
		map.put("min",(curr-1)*pbz.getSize() );
		map.put("max",pbz.getSize() );
		int count=bis_Shop_UserService.getCount(map);
		pbz.setCount(count);//  閿熸澃纭锋嫹褰曢敓鏂ゆ嫹
		
		List<Bis_Shop_User> listbis_shop_user=bis_Shop_UserService.listAll(map);
		model.addAttribute("listbis_shop_user", listbis_shop_user);
		List<Bis_Shop> listbis_shop=shopService.listAll(new HashMap<>());
		model.addAttribute("listbis_shop", listbis_shop);
		List<Bis_shop_post> listbis_shop_post=bis_Shop_PostService.listAll(new HashMap<>());
		model.addAttribute("listbis_shop_post", listbis_shop_post);
		
		model.addAttribute("pbz", pbz);
		return "zsy/bis_shop_userList";
	}
	
	//閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽挳
	@RequestMapping("/toadd")
	public String toadd(Model model){
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
		String str=sdf.format(new Date());
		Random random=new Random();
		int num= random.nextInt(9999)+1000;
		String id=str+num;
		model.addAttribute("id", id);
		
//		List<Bis_Shop_User> listbis_shop_user=bis_Shop_UserService.listAll(new HashMap<>());
//		model.addAttribute("listbis_shop_user", listbis_shop_user);
		List<Bis_Shop> listbis_shop=shopService.listAll(new HashMap<>());
		model.addAttribute("listbis_shop", listbis_shop);
		List<Bis_shop_post> listbis_shop_post=bis_Shop_PostService.listAll(new HashMap<>());
		model.addAttribute("listbis_shop_post", listbis_shop_post);
		return "zsy/bis_shop_userSave";
	}
	
	//閿熸枻鎷烽敓鏂ゆ嫹
	@RequestMapping("/save")
	public String save(Bis_Shop_User bis_Shop_User , @RequestParam("file")MultipartFile file ,HttpServletRequest request){
		String path=request.getRealPath("/upload");
		File file1=new File(path);
	
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
	
		File dir =new File(file1.getPath(),sdf.format(date));
		if (!dir.exists()) {
			//閿熸枻鎷烽敓鏂ゆ嫹閿熶茎鏌》鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹铏煎攪閿熶茎鏌拷
			dir.mkdirs();
		}
		File newfile=new File(dir.getPath(), file.getOriginalFilename());
		try {
			if (!newfile.exists()) {
				//閿熸枻鎷锋椂閿熸枻鎷烽敓鏂ゆ嫹涔嬮敓鏂ゆ嫹鍙敓鏂ゆ嫹涓�閿熸枻鎷烽敓绉哥鎷烽敓渚ョ》鎷�
				newfile.createNewFile();
		}
			//閿熸枻鎷穎ile閿熷彨纰夋嫹閿熸枻鎷烽敓鎹烽潻鎷烽敓鐙＄鎷穘ewfile閿熸枻鎷�
			file.transferTo(newfile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
        bis_Shop_User.setPhoto("/upload/"+dir.getName()+"/"+newfile.getName());
		
        //閿熸枻鎷烽敓鏂ゆ嫹
        String account=request.getParameter("account");
        String password=request.getParameter("password");
        Object object=pas(account, password);
        bis_Shop_User.setPassword(object.toString());
        
		bis_Shop_UserService.save(bis_Shop_User);
		return "redirect:/bis_shop_user/list";
	}
	
	//閿熺潾闈╂嫹鏃堕敓鏂ゆ嫹瑕侀敓鏂ゆ嫹
	@RequestMapping("/init/{shop_user_id}")
	public String init(@PathVariable("shop_user_id")String shop_user_id, Model model){
		Bis_Shop_User bis_Shop_User=bis_Shop_UserService.getById(shop_user_id);
		//System.out.println(bis_Shop_User.getBis_Shopshop_id().getShop_name());
		model.addAttribute("bis_Shop_User", bis_Shop_User);
		List<Bis_Shop> listbis_shop=shopService.listAll(new HashMap<>());
		model.addAttribute("listbis_shop", listbis_shop);
		
		List<Bis_shop_post> listbis_shop_post=bis_Shop_PostService.listAll(new HashMap<>());
		model.addAttribute("listbis_shop_post", listbis_shop_post);
		
		return "zsy/bis_shop_userEdit";
	}
	
	//閿熺潾闈╂嫹
	@RequestMapping("/update")
	public String update(Bis_Shop_User bis_Shop_User ,@RequestParam("file")MultipartFile file ,HttpServletRequest request){
		String path=request.getRealPath("/upload");
		File file1=new File(path);
	
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");

		File dir =new File(file1.getPath(),sdf.format(date));
		if (!dir.exists()) {
			//閿熸枻鎷烽敓鏂ゆ嫹閿熶茎鏌》鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹铏煎攪閿熶茎鏌拷
			dir.mkdirs();
		}
		if (file.getOriginalFilename()==null||file.getOriginalFilename()=="") {
			bis_Shop_User.setPhoto(request.getParameter("photoname"));
		}else {
			File newfile=new File(dir.getPath(), file.getOriginalFilename());
			try {
				if (!newfile.exists()) {
					//閿熸枻鎷锋椂閿熸枻鎷烽敓鏂ゆ嫹涔嬮敓鏂ゆ嫹鍙敓鏂ゆ嫹涓�閿熸枻鎷烽敓绉哥鎷烽敓渚ョ》鎷�
					newfile.createNewFile();
			}
				//閿熸枻鎷穎ile閿熷彨纰夋嫹閿熸枻鎷烽敓鎹烽潻鎷烽敓鐙＄鎷穘ewfile閿熸枻鎷�
				file.transferTo(newfile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			String name=file.getOriginalFilename();
			String endname=name.substring(name.lastIndexOf("."));
			name=sdf.format(date)+new Random().nextInt(10000)+endname;
			
			bis_Shop_User.setPhoto("/upload/"+dir.getName()+"/"+newfile.getName());
		    System.out.println("璺緞:"+"/upload/"+dir.getName()+"/"+newfile.getName());
		}
		
		bis_Shop_UserService.update(bis_Shop_User);
		return "redirect:/bis_shop_user/list";
	}
	
	//鍒犻敓鏂ゆ嫹
	@RequestMapping("/delete/{shop_user_id}")
	public String delete(@PathVariable("shop_user_id")String shop_user_id){
		bis_Shop_UserService.delete(shop_user_id);
		return "redirect:/bis_shop_user/list";
	}
	
	
	//閿熸枻鎷疯妯￠敓鏂ゆ嫹閿熸枻鎷疯閿熶茎鍑ゆ嫹閿熸枻鎷�
	public Map initMap( HttpServletRequest request ,HttpSession session){
		Map map=new HashMap<>();
		/*Bis_Shop_User bsu=(Bis_Shop_User)session.getAttribute("shopuser");
		System.out.println("user:"+bsu.getShop_id());
		map.put("shop_id", bsu.getShop_id());*/
		String qaccount=request.getParameter("qaccount");
		String qshop_id=request.getParameter("qshop_id");
		String qpost_code=request.getParameter("qpost_code");
		String qstatus=request.getParameter("qstatus");
		String qusername=request.getParameter("qusername");
		String qwork_number=request.getParameter("qwork_number");
		String qmobile=request.getParameter("qmobile");
		String qidno=request.getParameter("qidno");
		if (qaccount!=null&&!qaccount.equals("")) {
			map.put("qaccount", qaccount);
			request.setAttribute("qaccount", qaccount);
		}
		if (qshop_id!=null&&!qshop_id.equals("-1")) {
			map.put("qshop_id", qshop_id);
			request.setAttribute("qshop_id", qshop_id);
		}
		if (qpost_code!=null&&!qpost_code.equals("-1")) {
			map.put("qpost_code", qpost_code);
			request.setAttribute("qpost_code", qpost_code);
		}
		if (qstatus!=null&&!qstatus.equals("-1")) {
			map.put("qstatus", qstatus);
			request.setAttribute("qstatus", qstatus);
		}
		if (qusername!=null&&!qusername.equals("")) {
			map.put("qusername", qusername);
			request.setAttribute("qusername", qusername);
		}
		if (qwork_number!=null&&!qwork_number.equals("")) {
			map.put("qwork_number", qwork_number);
			request.setAttribute("qwork_number", qwork_number);
		}
		if (qmobile!=null&&!qmobile.equals("")) {
			map.put("qmobile", qmobile);
			request.setAttribute("qmobile", qmobile);
		}
		if (qidno!=null&&!qidno.equals("")) {
			map.put("qidno", qidno);
			request.setAttribute("qidno", qidno);
		}
		return map;
	}

	public Object pas(String account,String pas){
		String hashAlgorithName="MD5";
		//閿熸枻鎷峰��
		Object salt=ByteSource.Util.bytes(account);
		//閿熸枻鎷烽敓鏉拌揪鎷烽敓鏂ゆ嫹
		int count=1024;
		//閿熸枻鎷烽敓鍙》鎷烽敓鏂ゆ嫹
		Object result=new SimpleHash(hashAlgorithName, pas,salt,count);
		System.out.println("result:"+result);
		return result;
	}
	
	
	//鍓嶅彴鍛橀敓鏂ゆ嫹閿熶茎鍑ゆ嫹閿熸枻鎷�
	
	//鍛橀敓鏂ゆ嫹閿熷彨鎲嬫嫹鐗熼敓绐栵拷
		@RequestMapping("/listpfd")
		public String listpfd(Model model ,HttpServletRequest request ,@RequestParam(value="curr",defaultValue="1")int curr,HttpSession session){
			
			
			PageBeanZsy pbz=new PageBeanZsy();
			pbz.setCurrent(curr);
			Map<String, Object> map=initMap(request, null);
			pbz.setSize(3);  // 姣忛〉閿熸枻鎷风ず閿熸枻鎷烽敓鏂ゆ嫹
			//   min 閿熻鍖℃嫹濮嬮敓閾版唻鎷�   閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷� max  閿熻鏂ゆ嫹閿熸枻鎷烽敓閾版唻鎷�
			map.put("min",(curr-1)*pbz.getSize()+1 );
			map.put("max",pbz.getSize() );
			int count=bis_Shop_UserService.getCount(map);
			pbz.setCount(count);//  閿熸澃纭锋嫹褰曢敓鏂ゆ嫹
			
			Bis_Shop_User bsu=(Bis_Shop_User)session.getAttribute("shopuser");
			map.put("account", bsu.getAccount());
			
			List<Bis_Shop_User> listbis_shop_user=bis_Shop_UserService.getByShopId(map);
			System.out.println("111111112222:"+listbis_shop_user.size());
			model.addAttribute("listbis_shop_user", listbis_shop_user);
			
			List<Bis_Shop> listbis_shop=shopService.listAll(new HashMap<>());
			model.addAttribute("listbis_shop", listbis_shop);
			
			List<Bis_shop_post> listbis_shop_post=bis_Shop_PostService.listAll(new HashMap<>());
			model.addAttribute("listbis_shop_post", listbis_shop_post);
			
			model.addAttribute("pbz", pbz);
			return "zsy/bis_shop_userListpfd";
		}
		
		
		//閿熶粙鐪嬮敓鏂ゆ嫹閿熸枻鎷锋椂閿熸枻鎷疯閿熸枻鎷�
		@RequestMapping("/initpfd/{shop_user_id}")
		public String initpfd(@PathVariable("shop_user_id")String shop_user_id, Model model){
			Bis_Shop_User bis_Shop_User=bis_Shop_UserService.getById(shop_user_id);
			model.addAttribute("bis_Shop_User", bis_Shop_User);
			
			List<Bis_Shop> listbis_shop=shopService.listAll(new HashMap<>());
			model.addAttribute("listbis_shop", listbis_shop);
			
			List<Bis_shop_post> listbis_shop_post=bis_Shop_PostService.listAll(new HashMap<>());
			model.addAttribute("listbis_shop_post", listbis_shop_post);
			return "zsy/bis_shop_userSee";
		}
		
	

}
